/**
 * Created by Windows on 2015/11/20.
 */

$(document).on('click', '.overlay-close', function() {
    $(".remove").remove();
});

$(document).on('click', '.delete-supply', function() {
    $(this).parents("li").remove();
});

$(function(){

    //首页的JS
    $(document).delegate(".change_mcode", "click", function(e){
        var img = document.getElementById('login_code_img');
        img.src = '/login/code/rand/' + Math.random();
    });

    $(document).delegate('.buy',"click", function(e) {
        //alert("1");/shopcart/business?id="+json[list_index]['id']+"
        var quotetaion_id = $(this).attr("buy_id");
        $.ajax({
            url:"/json/checklogion",
            data:{
                quotetaion_id:quotetaion_id
            },
            type:'post',
            dataType:'json',
            success:function(data){
                var html="";
                if(data=='8'){
                    window.location.href="/shopcart/business?id="+quotetaion_id;
                }else{
                    html+="<div class=\"mui-dialog-mask remove\"></div>";
                    html+="<div class=\"mui-dialog remove\" style=\"width:500px;height:390px;\">";
                        html+="<a href=\"javascript:void(0)\" class=\"overlay-close\"></a>";
                        html+="<div class=\"mui-diglog-wrap\">";
                            html+="<div class=\"mui-dialog-header\">用户登录</div>";
                            html+="<div class=\"mui-dialog-body\">";
                                html+="<ul class=\"mui_dl_list\">";
                                    html+="<li class='tip orange' style='padding:0;padding-left:70px;height: 21px;'></li>";
                                    html+="<li>手机号码：<input class=\"popup_text\" name='mobile' id='mobile' type=\"text\" placeholder=\"请输入手机号码\"/></li>";
                                    html+="<li>密　　码：<input class=\"popup_text\" name='password' id='password' type=\"password\" placeholder=\"请输入密码\"/></li>";
                                    html+="<li>　验证码：<input class=\"popup_text w150\" name='code' id='code' type=\"text\" placeholder=\"请输入验证码\"/><img width=\"80\" height=\"34\" class=\"ml10 vam change_mcode\" id=\"login_code_img\" src=\"/login/code\"></li>";
                                    html+="<li class=\"pl30p\"><input class=\"popup_btn\" type=\"button\" value=\"登 录\"/></li>";
                                html+="</ul>";
                                html+="<h3 class=\"login-sd\"><i>第三方账号登录</i></h3>";
                                html+="<p class=\"login-sdn\">";
                                    html+="<a title=\"QQ登录\" class=\"ml5p\" href=\"/app_login/qq_login.html\"><img alt=\"QQ登录\" src=\"/images/login_qq.jpg\"></a>";
                                    html+="<a title=\"新浪登录\" class=\"ml5p\" href=\"/app_login/sina_login.html\"><img src=\"/images/sian.jpg\"></a>";
                                html+="</p>";
                            html+="</div>";
                        html+="</div>";
                        html+="<div class=\"mui-dialog-skin\"></div>";
                    html+="</div>";
                    $("body").append(html);
                }
            }

        });

    });

    $(document).delegate(".popup_btn", "click", function(e){
        var mobile = $("#mobile").val();
        var password = $("#password").val();
        var code = $("#code").val();
        var back_url = window.location.href;
        $.ajax({
            url:"/login/login",
            data:{
                username:mobile,
                password:password,
                code:code,
                back_url:back_url,
            },
            type:"post",
            dataType:"json",
            success: function (data) {
                var ccode= data.code;
                var msg= data.msg;
                if(ccode==1008){
                    window.location.href = back_url;
                }else{
                    $(".mui_dl_list .tip").html(msg);
                    return false;
                }
            }
        })

    })

});